package tools.packet;

import handling.opcode.SendPacketOpcode;
import server.MapleSignin;
import tools.data.output.MaplePacketLittleEndianWriter;

import java.util.Map;

import static tools.packet.SigninPacket.SIGNIN_TYPE.领取奖励;

public class SigninPacket {

    public static byte[] showSigninUI(final int mode, final int operation, final int itemid, final int xd) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

        mplew.writeShort(SendPacketOpcode.SIGIN_INFO.getValue());

        mplew.write(mode);
        if (mode > 0) {
            if (mode > 1) {
                if (mode == 2) {
                    mplew.writeInt(operation);
                    mplew.writeInt(itemid);
                    if (operation == 14) {
                        mplew.writeInt(xd);
                    }
                }
            } else {
                mplew.writeInt(0);
            }
        } else {
            MapleSignin.SiginMonth siginMonth = MapleSignin.getInstance().getSiginMonth();
            mplew.writeBool(siginMonth != null);
            if (siginMonth != null) {
                mplew.writeLong(PacketHelper.getTime(-2));
                mplew.writeLong(PacketHelper.getTime(-1));
                mplew.writeInt(siginMonth.getDate());
                mplew.writeInt(0);
                mplew.writeInt(siginMonth.getRewards().size());
                siginMonth.getRewards().forEach((key, value) -> {
                    mplew.writeInt(key);
                    mplew.writeInt(value.getItemId());
                    mplew.writeInt(value.getQuantity());
                    if (value.getExpiredate() > 0) {
                        mplew.writeInt(1);
                        mplew.writeInt(value.getExpiredate());
                    } else {
                        mplew.writeLong(0);
                    }
                    mplew.writeInt(value.getIsCash());
                    mplew.write(0);
                    mplew.writeInt(0);
                    mplew.write(0);
                });
                mplew.writeInt(MapleSignin.MINLEVEL);
                mplew.writeInt(MapleSignin.getInstance().getUnknownMap().size());
                for (Map.Entry<Integer, Integer> integerEntry : MapleSignin.getInstance().getUnknownMap().entrySet()) {
                    mplew.writeInt(integerEntry.getKey());
                    mplew.writeInt(integerEntry.getValue());
                }
                mplew.writeInt(0);
            }
            mplew.writeInt(0);
        }

        return mplew.getPacket();
    }

    public static byte[] getSigninReward(int itemid) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();

        mplew.writeShort(SendPacketOpcode.SIGIN_INFO.getValue());

        mplew.write(1);
        mplew.writeInt(领取奖励.ordinal());
        mplew.writeInt(itemid);

        return mplew.getPacket();
    }

    enum SIGNIN_TYPE {
        UNKNOWN,
        签到窗口,
        领取奖励,;
    }
}
